/**
 * 
 */
package com.scpii.universal.struts.manage;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.doone.comm.paginaction.Pagination;
import com.scpii.universal.action.BaseAction;
import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.hibernate.entity.PushMessageTab;
import com.scpii.universal.service.PushMessageService;
import com.scpii.universal.service.TypeService;
import com.scpii.universal.util.UtilTool;

/**
 * universalWeb
 * 
 * @author weny 2012-7-9下午05:20:49
 */
@Controller
@Scope("prototype")
public class MessageAction extends BaseAction {
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 4629317011625914415L;
	@Resource
	private PushMessageService pushMessageService;

	@Resource
	private TypeService typeService;

	private Pagination page = new Pagination();

	private List<PushMessageTab> messagelist;

	private PushMessageTab messageInfo;

	private PushMessageTab pushmessageInfo;

	// 获取消息类型
	private Map<Integer, String> msgTypeMap;

	private Map<Integer, String> msgStatusMap;

	public String getMessagePage() {
		// modify by zhangqiang 20120725
		// UserInfoDto userInfoDto=(UserInfoDto)
		// session.getAttribute("userInfoDto");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("intAppId", getUserInfo().getAppId());
		try {

			// 获取返回信息的条数
			Integer recordCount = pushMessageService.getMessageRecode(map);

			if (getSelectPerPage("page") == -1) {
				page.setPageRecord(GenericConstants.PAGE_NO);
			} else {
				page.setPageRecord(getSelectPerPage("page"));
			}
			// 判断是否是第一次查询
			if (getQueryFlag("page") == -1) {
				page.setTotalRecord(recordCount);
			} else {
				page.setTotalRecord(getQueryFlag("page"));
			}
			setPageParameters(page, "page");
			// 获取页面返回list
			messagelist = pushMessageService.getMessageListPage(map,
					page.getCurrentPage(), page.getPageStart(),
					page.getPageRecord());

			// 获取消息类型
			msgTypeMap = typeService
					.getTypeListByParentId(GenericConstants.PUSH_MSG_TYPE_ID);
			// 推动消息状态
			msgStatusMap = typeService
					.getTypeListByParentId(GenericConstants.PUSH_MSG_STATUS_ID);

			if (messagelist != null && messagelist.size() > 0) {
				for (PushMessageTab tab : messagelist) {
					tab.setVarPushMsgStatueName(msgStatusMap.get((int)tab
							.getIntPushMsgStatue()));
					tab.setVarPushMsgTypeName(msgTypeMap.get((int)tab
							.getIntPushMsgType()));
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 添加消息
	 * 
	 * @return
	 * @author weny 2012-7-13上午09:54:43
	 */
	public String addMessage() {
		// UserInfoDto userInfoDto=(UserInfoDto)
		// session.getAttribute("userInfoDto");
		String dateString = UtilTool.dateToString(new Date());
		pushmessageInfo.setDatUpdateDatetime(dateString);
		pushmessageInfo.setIntDeleteSign(GenericConstants.DELETE_SIGN_EXIST);
		pushmessageInfo.setIntAppId(getUserInfo().getAppId());
		pushMessageService.save(pushmessageInfo);
		return SUCCESS;

	}

	/**
	 * 删除消息
	 * 
	 * @return
	 * @author weny 2012-7-13上午09:58:47
	 */
	public String delMessage() {
		String messageId = request.getParameter("mesId");
		pushMessageService.updateDeleteSign(Integer.valueOf(messageId));
		return SUCCESS;
	}

	/**
	 * 获取消息
	 * 
	 * @return
	 * @author weny 2012-7-13上午10:00:26
	 */
	public String getMessage() {
		String messageId = request.getParameter("mesId");
		messageInfo = pushMessageService.get(Integer.valueOf(messageId));
		return SUCCESS;
	}

	/**
	 * 编辑消息
	 * 
	 * @return
	 * @author weny 2012-7-13上午10:13:19
	 */
	public String editMessage() {
		// modify by zhangqiang 20120725
		// UserInfoDto userInfoDto=(UserInfoDto)
		// session.getAttribute("userInfoDto");
		messageInfo.setIntAppId(getUserInfo().getAppId());
		messageInfo.setIntDeleteSign(GenericConstants.DELETE_SIGN_EXIST);
		messageInfo.setDatUpdateDatetime(UtilTool.dateToString(new Date()));
		pushMessageService.update(messageInfo);
		return SUCCESS;
	}

	public Pagination getPage() {
		return page;
	}

	public void setPage(Pagination page) {
		this.page = page;
	}

	public List<PushMessageTab> getMessagelist() {
		return messagelist;
	}

	public void setMessagelist(List<PushMessageTab> messagelist) {
		this.messagelist = messagelist;
	}

	public PushMessageTab getMessageInfo() {
		return messageInfo;
	}

	public void setMessageInfo(PushMessageTab messageInfo) {
		this.messageInfo = messageInfo;
	}

	public PushMessageTab getPushmessageInfo() {
		return pushmessageInfo;
	}

	public void setPushmessageInfo(PushMessageTab pushmessageInfo) {
		this.pushmessageInfo = pushmessageInfo;
	}

	public Map<Integer, String> getMsgTypeMap() {
		return msgTypeMap;
	}

	public void setMsgTypeMap(Map<Integer, String> msgTypeMap) {
		this.msgTypeMap = msgTypeMap;
	}

	public Map<Integer, String> getMsgStatusMap() {
		return msgStatusMap;
	}

	public void setMsgStatusMap(Map<Integer, String> msgStatusMap) {
		this.msgStatusMap = msgStatusMap;
	}

}
